package com.kaka.bus.engine.home.main.limit;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.BpmList;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.ProductDetailBean;
import com.kaka.bus.bean.Status;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.constant.UserConstant;
import com.kaka.bus.di.component.DaggerLoanComponent;
import com.kaka.bus.engine.amount.loanproduct.LoanProductActivity;
import com.kaka.bus.engine.borrow.face.FaceActivity;
import com.kaka.bus.engine.borrow.idcard.IdCardBackActivity;
import com.kaka.bus.engine.borrow.idcard.IdCardFrontActivity;
import com.kaka.bus.engine.borrow.person.PersonActivity;
import com.kaka.bus.engine.borrow.phone.PhoneActivity;
import com.kaka.bus.util.U;
import com.kaka.bus.view.AuthView;
import com.kaka.bus.view.LimitView;
import com.kaka.bus.view.NumberView;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by OnionMac on 2018/7/30.
 */

public class LimitActivity extends BaseActivity<LimitPresenter> implements LimitContract.View {


    public static final String LIMIT = "limit";
    public static final String PRODUCTID = "productId";
    public final static int REQUESTCODE = 1; // 返回的结果码
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar_rl)
    RelativeLayout mToolbarRl;
    @BindView(R.id.limit_idcard)
    AuthView mLimitIdcard;
    @BindView(R.id.limit_person)
    AuthView mLimitPerson;
    @BindView(R.id.limit_face)
    AuthView mLimitFace;
    @BindView(R.id.limit_phone)
    AuthView mLimitPhone;
    @BindView(R.id.limit_arc)
    LimitView mLimitArc;
    @BindView(R.id.limit_number)
    NumberView mLimitNumber;
    @BindView(R.id.limit_swipe)
    SwipeRefreshLayout mLimitSwipe;
    @BindView(R.id.ll_product)
    LinearLayout ll_product;

    @BindView(R.id.btn_next)
    Button btn_next;

    private LinkedList<AuthView> mAuthViews = new LinkedList<>();
    private List<BpmList> mData;
    private float mLimit;
    String mProductId;
    private float money;
    private HttpWrapper<ProductDetailBean> beanHttpWrapper;

    @Override
    protected void initView() {
        super.initView();

        mLimit = getIntent().getFloatExtra(LIMIT, 0);
        mProductId = getIntent().getStringExtra(PRODUCTID);
        ll_product.setVisibility(View.GONE);
        /**
         * swipe的下拉高度
         */
        mLimitSwipe.setProgressViewEndTarget(false, 300);
        mLimitSwipe.setColorSchemeColors(getResources().getColor(R.color.toolbar_color));

        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("信用额度");

        mAuthViews.addLast(mLimitIdcard);
        mAuthViews.addLast(mLimitPerson);
        mAuthViews.addLast(mLimitFace);
        mAuthViews.addLast(mLimitPhone);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initDataOnStart() {
        super.initDataOnStart();
        mPresenter.bpmList(getUserId(), getToken(), mProductId);
        mPresenter.checkBpm(getUserId(), getToken());
        mPresenter.recommend(getUserId(), getToken(), Constant.PLATFORM, U.getAppInfo(this).getVersionName());
    }

    @Override
    public void checkBpmSuccess(HttpWrapper<Status> s) {
        if (Constant.TOKEN_TIMEOUT.equals(s.code)) {
            U.tokenOut(this);
        } else if (Constant.NODE_OK.equals(s.code) || Constant.SUCCESS_CODE.equals(s.code)) {
            money = Float.parseFloat(s.getData().getAmount());
            Intent intent = new Intent();
            intent.putExtra("money", money); //将计算的值回传回去
            setResult(REQUESTCODE, intent);
            mLimitNumber.setData(0, (int) money);
            mLimitNumber.start();
            mLimitArc.start();
        } else {
            mLimitNumber.setData(0, 0);
            mLimitNumber.start();
            mLimitArc.start();
        }
    }

    @Override
    public void recommendSuccess(HttpWrapper<ProductDetailBean> wrapper) {
        if (Constant.SUCCESS_CODE.equals(wrapper.getCode())) {
            if (null != wrapper.getData()) {
                beanHttpWrapper = wrapper;
                ll_product.setVisibility(View.VISIBLE);
            } else {
                ll_product.setVisibility(View.GONE);
            }
        } else {
            ll_product.setVisibility(View.GONE);
        }

    }

    @Override
    public void bpmListSuccess(HttpWrapper<List<BpmList>> wrapper) {
        closeRefresh();
        if (Constant.SUCCESS_CODE.equals(wrapper.code)) {
            mData = wrapper.getData();
            if (mData == null) {
                return;
            }
            for (int i = 0; i < mData.size(); i++) {
                mAuthViews.get(i).setTips(mData.get(i).getNodeStatusName(), mData.get(i).getNodeStatus());
            }
        } else {
            showMessage(wrapper.getInfo());
        }
    }

    @Override
    protected void initListener() {
        for (int i = 0; i < mAuthViews.size(); i++) {
            int finalI = i;
            mAuthViews.get(i).setOnClickListener(v -> {
                if (mData == null) {
                    showMessage("信息获取失败,请下拉刷新一下");
                    return;
                }
                BpmList bpmList = mData.get(finalI);
                String nodeStatus = bpmList.getNodeStatus();
                if (UserConstant.NS001.equals(nodeStatus)) {
                    switch (finalI) {
                        case 0:
                            if (1 == bpmList.getNodeId()) {
                                startActivity(new Intent(LimitActivity.this, IdCardFrontActivity.class));
                            } else if (2 == bpmList.getNodeId()) {
                                startActivity(new Intent(LimitActivity.this, IdCardBackActivity.class));
                            }
                            break;
                        case 1:
                            startActivity(new Intent(LimitActivity.this, PersonActivity.class));
                            break;
                        case 2:
                            startActivity(new Intent(LimitActivity.this, FaceActivity.class));
                            break;
                        case 3:
                            startActivity(new Intent(LimitActivity.this, PhoneActivity.class));
                            break;
                    }
                } else if (UserConstant.NS002.equals(nodeStatus)) {
                    showMessage("该认证已经完成");
                } else if (UserConstant.NS004.equals(nodeStatus)) {
                    showMessage("认证在审核中,请耐心等待结果");
                } else if (UserConstant.NS006.equals(nodeStatus)) {
                    showMessage("请先完成以上认证");
                }
            });
        }

        mLimitSwipe.setOnRefreshListener(this::initDataOnStart);
        btn_next.setOnClickListener(v -> {

            Intent intent = new Intent(LimitActivity.this, LoanProductActivity.class);
            intent.putExtra(LoanProductActivity.TYPE, 1);
            intent.putExtra(LoanProductActivity.PROID, beanHttpWrapper.getData().getProductId());
            startActivity(intent);
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_limit;
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    public void closeRefresh() {
        if (mLimitSwipe == null) {
            return;
        }

        if (mLimitSwipe.isRefreshing()) {
            mLimitSwipe.setRefreshing(false);
        }
    }

    @Override
    protected void initInject() {
        DaggerLoanComponent.builder().activityComponent(getActivityComponent()).build().inject(this);
    }

}
